package com.farproc.wifi.connecter;

import android.app.ListActivity;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Toast;
import android.widget.TwoLineListItem;

import com.arashivision.wifisdk.WifiSdk;
import com.arashivision.wifisdk.WifiSdkCallback;

import java.util.List;

/**
 * Created by vans on 4/6/18.
 */

public class TestWifiActivity extends ListActivity
{
    private static final String TAG = "TestWifiActivity";
    private WifiSdk mWifiManager;
    private List<ScanResult> mScanResults;

    private long mStartTimeMs = 10 * 1000;

    private void showToast(String str)
    {
        Log.d(TAG,str);
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(mListAdapter);
        getListView().setOnItemClickListener(mItemOnClick);

        if(mWifiManager.getConnectWifiInfo() != null)
        {
            Log.d(TAG,"connect wifi info " + mWifiManager.getConnectWifiInfo().getSSID());
        }
        else
        {
            Log.d(TAG,"connect wifi info null");
        }

        mWifiManager = new WifiSdk(this.getApplicationContext(), new WifiSdkCallback()
        {
            @Override
            public void onScanResult(List<ScanResult> listResults)
            {
                mScanResults = listResults;
                Log.d(TAG," mScanResults size " + mScanResults.size());
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        mListAdapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onConnectSuc(ScanResult result)
            {
                showToast("connect suc " + result.SSID);
            }

            @Override
            public void onConnectFail(ScanResult result)
            {
                showToast("onConnectFail " + result.SSID);
            }

            @Override
            public void onConnectRequirePwd(ScanResult result)
            {
                showToast("onConnectRequirePwd " + result.SSID);
            }

            @Override
            public void onNotSupport(ScanResult result)
            {
                showToast("onNotSupport " + result.SSID);
            }

            @Override
            public void onForgetSuc(ScanResult result)
            {
                showToast("onForgetSuc " + result.SSID);
            }

            @Override
            public void onForgetFail(ScanResult result)
            {
                showToast("onForgetFail " + result.SSID);
            }

            @Override
            public void changeWifiPwdSuc(ScanResult result)
            {
                showToast("changeWifiPwdSuc " + result.SSID);
            }

            @Override
            public void changeWifiPwdFail(ScanResult result)
            {
                showToast("changeWifiPwdFail " + result.SSID);
            }
        },null);

        mWifiManager.setEnableDebug(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG,"mWifiManager.isWifiEnabled() " + mWifiManager.getWifiState());
        if(!mWifiManager.getWifiState())
        {
            mWifiManager.openWifi();
        }
        Log.d(TAG,"2mWifiManager.isWifiEnabled() " + mWifiManager.getWifiState());
        mWifiManager.startScan(mStartTimeMs);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private int calculateSignal(int rssi, int numLevels)
    {
        return mWifiManager.calculateSignal(rssi, numLevels);
    }

    private BaseAdapter mListAdapter = new BaseAdapter() {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null || !(convertView instanceof TwoLineListItem)) {
                convertView = View.inflate(getApplicationContext(),
                        android.R.layout.simple_list_item_2, null);
            }

            final ScanResult result = mScanResults.get(position);
            ((TwoLineListItem)convertView).getText1().setText(result.SSID);
            ((TwoLineListItem)convertView).getText2().setText(
                    String.format("%s  %d", result.BSSID, calculateSignal(result.level,5))
            );
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public int getCount() {
            return mScanResults == null ? 0 : mScanResults.size();
        }
    };

    private AdapterView.OnItemClickListener mItemOnClick = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            final ScanResult result = mScanResults.get(position);
            mWifiManager.connect(result);
        }
    };
}
